package com.tdam2011.grupo05.ui.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tdam2011.grupo05.R;
import com.tdam2011.grupo05.control.app.TdamApplication;
import com.tdam2011.grupo05.entities.account.Account;
import com.tdam2011.grupo05.services.ServiceHelper;

public class LoginActivity extends Activity {

	final static String TAG = "LoginActivity";

	private TextView mTextViewUsuario;
	private TextView mteTextViewPass;

	private Button mButtonAceptar;
	private Button mButtonCancelar;

	private ServiceHelper mServiceHelper;

	private ProgressDialog mProgressDialog;

	private final Handler mHandler = new Handler();

	private View.OnClickListener okAction = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			doLogin();
		}
	};

	private View.OnClickListener cancelAction = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			setResultLogin(false);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		mProgressDialog = new ProgressDialog(this);

		mProgressDialog.setTitle(getResources().getString(
				R.string.progress_dialog_wait_message));

		mProgressDialog.setMessage("Iniciando Sesion");

		mServiceHelper = ServiceHelper.getIntance(this);

		mTextViewUsuario = (TextView) findViewById(R.id.txt_usuario_login);
		mteTextViewPass = (TextView) findViewById(R.id.txt_password_login);

		mButtonAceptar = (Button) findViewById(R.id.btn_ok_login);
		mButtonCancelar = (Button) findViewById(R.id.btn_cancelar_login);

		mButtonAceptar.setOnClickListener(okAction);

		mButtonCancelar.setOnClickListener(cancelAction);

	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	public void doLogin() {
		final String usuario = mTextViewUsuario.getText().toString();
		final String password = mteTextViewPass.getText().toString();
		mProgressDialog.show();
		Runnable r = new Runnable() {

			@Override
			public void run() {
				final Account account = mServiceHelper.getDataManager().get(
						usuario, password);
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					Log.e(TAG, "InterruptedException", e);
				}
				mProgressDialog.dismiss();
				if (account != null) {

					mHandler.post(new Runnable() {

						@Override
						public void run() {
							mProgressDialog.dismiss();
							((TdamApplication) getApplication())
									.updateCurrentAccount(account);

						}
					});
					setResultLogin(true);
				} else {
					mHandler.post(new Runnable() {

						@Override
						public void run() {
							mProgressDialog.dismiss();
							AlertDialog.Builder alert = new AlertDialog.Builder(
									LoginActivity.this);
							alert.setTitle("Error");
							alert.setMessage("Usuario/Pass no existen");
							alert.setPositiveButton("Ok",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											mteTextViewPass.setText("");
											mTextViewUsuario.setText("");
										}
									}).create().show();
						}
					});
				}
			}
		};
		new Thread(r).start();

	}

	private void setResultLogin(boolean result) {
		if (result) {
			setResult(RESULT_OK);
		} else {
			setResult(RESULT_CANCELED);
		}
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_settings:

			break;
		case R.id.menu_salir:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {

	}

}
